<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/20
 * Time: 11:00
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\MarketService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class MarketController extends BaseController
{
    protected $service;

    public function __construct(Request $request, MarketService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 申请退款
     * @return \Illuminate\Http\JsonResponse
     */
    public function refundMarket()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'detail_id'     =>  'required|integer|exists:mini_order_product,id',
            'is_receive'    =>  'required|in:0,1',
            'refund_reason' =>  'required|string|max:100',
            'refund_remark' =>  'string|max:500',
            'refund_proof'  =>  'array'
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在',
            'detail_id.required'    =>  '订单商品ID不能为空',
            'detail_id.integer'     =>  '订单商品ID必须为整型',
            'detail_id.exists'      =>  '订单商品不存在',
            'is_receive.required'   =>  '是否收货不能为空',
            'is_receive.in'         =>  '是否收货选择错误',
            'refund_reason.required'=>  '退款原因不能为空',
            'refund_reason.string'  =>  '退款原因必须为字符',
            'refund_reason.max'     =>  '退款原因过长',
            'refund_remark.string'  =>  '退款备注必须为字符',
            'refund_remark.max'     =>  '退款备注过长',
            'refund_proof.array'    =>  '退款凭证必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_MARKET_REFUND_ERROR, $validator->errors()->first());
        }

        $order_number   =   $this->request->post('order_number');
        [$flag, $result]    =   $this->service->marketRefund($this->request->post(), $order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_MARKET_REFUND_ERROR, $result);
        }
    }

    /**
     * 申请退货
     * @return \Illuminate\Http\JsonResponse
     */
    public function returnMarket()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'detail_id'     =>  'required|integer|exists:mini_order_product,id',
            'is_receive'    =>  'required|in:0,1',
            'is_user_send'  =>  'required|in:0,1',
            'refund_reason' =>  'required|string|max:100',
            'refund_remark' =>  'string|max:500',
            'refund_proof'  =>  'array',
            'send_express'  =>  'exists:mini_express,id',
            'send_express_no'  =>  'string|max:50',
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在',
            'detail_id.required'    =>  '订单商品ID不能为空',
            'detail_id.integer'     =>  '订单商品ID必须为整型',
            'detail_id.exists'      =>  '订单商品不存在',
            'is_receive.required'   =>  '是否收货不能为空',
            'is_receive.in'         =>  '是否收货选择错误',
            'is_user_send.required' =>  '是否发货不能为空',
            'is_user_send.in'       =>  '是否发货选择错误',
            'refund_reason.required'=>  '退款原因不能为空',
            'refund_reason.string'  =>  '退款原因必须为字符',
            'refund_reason.max'     =>  '退款原因过长',
            'refund_remark.string'  =>  '退款备注必须为字符',
            'refund_remark.max'     =>  '退款备注过长',
            'refund_proof.array'    =>  '退款凭证必须为数组',
            'send_express.exists'   =>  '发货公司不存在',
            'send_express_no.string'=>  '发货单号必须为字符',
            'send_express_no.max'   =>  '发货单号过长'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_MARKET_RETURN_ERROR, $validator->errors()->first());
        }

        $order_number   =   $this->request->post('order_number');
        [$flag, $result]    =   $this->service->marketReturn($this->request->post(), $order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_MARKET_RETURN_ERROR, $result);
        }
    }

    /**
     * 用户申请退款退货
     * @return \Illuminate\Http\JsonResponse
     */
    public function exchangeMarket()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'detail_id'     =>  'required|integer|exists:mini_order_product,id',
            'is_receive'    =>  'required|in:0,1',
            'is_user_send'  =>  'required|in:0,1',
            'refund_reason' =>  'required|string|max:100',
            'refund_remark' =>  'string|max:500',
            'refund_proof'  =>  'array',
            'address_id'    =>  'required|exists:mini_user_addres,id',
            'send_express'  =>  'exists:mini_express,id',
            'send_express_no'  =>  'string|max:50',
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在',
            'detail_id.required'    =>  '订单商品ID不能为空',
            'detail_id.integer'     =>  '订单商品ID必须为整型',
            'detail_id.exists'      =>  '订单商品不存在',
            'is_receive.required'   =>  '是否收货不能为空',
            'is_receive.in'         =>  '是否收货选择错误',
            'is_user_send.required' =>  '是否发货不能为空',
            'is_user_send.in'       =>  '是否发货选择错误',
            'refund_reason.required'=>  '退款原因不能为空',
            'refund_reason.string'  =>  '退款原因必须为字符',
            'refund_reason.max'     =>  '退款原因过长',
            'refund_remark.string'  =>  '退款备注必须为字符',
            'refund_remark.max'     =>  '退款备注过长',
            'refund_proof.array'    =>  '退款凭证必须为数组',
            'send_express.exists'   =>  '发货公司不存在',
            'send_express_no.string'=>  '发货单号必须为字符',
            'send_express_no.max'   =>  '发货单号过长',
            'address_id.required'   =>  '收货地址不能为空',
            'address_id.exists'     =>  '收货地址不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_MARKET_RETURN_ERROR, $validator->errors()->first());
        }

        $order_number   =   $this->request->post('order_number');
        [$flag, $result]    =   $this->service->marketExchange($this->request->post(), $order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_MARKET_RETURN_ERROR, $result);
        }
    }

    /**
     * 查看售后订单进度
     * @return \Illuminate\Http\JsonResponse
     */
    public function processMarket()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'detail_id'     =>  'required|integer|exists:mini_order_product,id',
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在',
            'detail_id.required'    =>  '订单商品ID不能为空',
            'detail_id.integer'     =>  '订单商品ID必须为整型',
            'detail_id.exists'      =>  '订单商品不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_MARKET_PROCESS_ERROR, $validator->errors()->first());
        }

        $order_number   =   $this->request->post('order_number');
        $detail_id      =   (int) $this->request->post('detail_id');
        [$flag, $result]    =   $this->service->marketProcess($order_number, $detail_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_MARKET_PROCESS_ERROR, $result);
        }
    }

    /**
     * 售后订单详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailMarket()
    {
        $validator  =   Validator::make($this->request->post(), [
            'refund_no' =>  'required|string',  //|exists:mini_order_refund'
        ], [
            'refund_no.required'    =>  '售后单号不能为空',
            'refund_no.string'      =>  '售后单号必须为字符',
            'refund_no.exists'      =>  '售后单号不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_MARKET_DETAIL_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->marketDetail($this->request->post('refund_no'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_MARKET_DETAIL_ERROR, $result);
        }
    }

    /**
     * 用户取消售后操作
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancelMarket()
    {
        $validator  =   Validator::make($this->request->post(), [
            'refund_no' =>  'required|string',  //|exists:mini_order_refund'
        ], [
            'refund_no.required'    =>  '售后单号不能为空',
            'refund_no.string'      =>  '售后单号必须为字符',
            'refund_no.exists'      =>  '售后单号不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_MARKET_CANCEL_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->marketCancel($this->request->post('refund_no'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_MARKET_CANCEL_ERROR, $result);
        }
    }

    /**
     * 用户修改售后订单内容
     * @return \Illuminate\Http\JsonResponse
     */
    public function modifyMarket()
    {
        $validator  =   Validator::make($this->request->post(), [
            'refund_no'     =>  'required|string|exists:mini_order_refund,order_no',
            'is_receive'    =>  'required|in:0,1',
            'refund_reason' =>  'required|string|max:100',
            'refund_remark' =>  'string|max:500',
            'refund_proof'  =>  'array'
        ], [
            'refund_no.required'    =>  '订单号不能为空',
            'refund_no.string'      =>  '订单号必须为字符',
            'refund_no.exists'      =>  '订单不存在',
            'is_receive.required'   =>  '是否收货不能为空',
            'is_receive.in'         =>  '是否收货选择错误',
            'refund_reason.required'=>  '退款原因不能为空',
            'refund_reason.string'  =>  '退款原因必须为字符',
            'refund_reason.max'     =>  '退款原因过长',
            'refund_remark.string'  =>  '退款备注必须为字符',
            'refund_remark.max'     =>  '退款备注过长',
            'refund_proof.array'    =>  '退款凭证必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_MARKET_MODIFY_ERROR, $validator->errors()->first());
        }

        $refund_no   =   $this->request->post('refund_no');
        [$flag, $result]    =   $this->service->marketModify($this->request->post(), $refund_no);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_MARKET_MODIFY_ERROR, $result);
        }
    }
}
